package com.seu.project.seuquiz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seu.project.seuquiz.common.ErrorCode;
import com.seu.project.seuquiz.constant.CommonConstant;
import com.seu.project.seuquiz.exception.BusinessException;
import com.seu.project.seuquiz.mapper.QuestionMapper;
import com.seu.project.seuquiz.model.dto.question.QuestionAddRequest;
import com.seu.project.seuquiz.model.dto.question.QuestionQueryRequest;
import com.seu.project.seuquiz.model.entity.CollegeContestMap;
import com.seu.project.seuquiz.model.entity.Question;
import com.seu.project.seuquiz.service.QuestionService;
import com.seu.project.seuquiz.util.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.data.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author lenovo
* @description 针对表【question(题目)】的数据库操作Service实现
* @createDate 2023-07-15 13:47:58
*/
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question>
    implements QuestionService {

    @Resource
    private QuestionMapper questionMapper;

    @Override
    public boolean questionUserAdd(int id, boolean isRight) {
        Question question = questionMapper.selectById(id);
        if (question == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "题目不存在");
        }

        question.setWriteCnt(question.getWriteCnt() + 1);
        if (isRight) {
            question.setCorrectCnt(question.getCorrectCnt() + 1);
        }

        return this.updateById(question);
    }

    @Override
    public QueryWrapper<Question> getQueryWrapper(QuestionQueryRequest request) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        if (request == null) {
            return null;
        }

        Long id = request.getId();
        String content = request.getContent();
        Integer score = request.getScore();
        String questionType = request.getQuestionType();
        String type = request.getType();
        Integer difficulty = request.getDifficulty();
        Integer correctCnt = request.getCorrectCnt();
        Integer writeCnt = request.getWriteCnt();
        Integer status = request.getStatus();
//        Date createTime = request.getCreateTime();
//        Date updateTime = request.getUpdateTime();
        String sortField = request.getSortField();
        String sortOrder = request.getSortOrder();

        queryWrapper.eq(id != null && id > 0,"id", id);
        queryWrapper.like(Strings.isNotBlank(content), "content", "%" + content + "%");
        queryWrapper.eq(score != null && score >= 0,"score", score);
        queryWrapper.like(Strings.isNotBlank(questionType), "questionType", "%" +questionType + "%");
        queryWrapper.like(Strings.isNotBlank(type), " type",  "%" + type + "%");
        queryWrapper.eq(difficulty != null && difficulty >= 0,"difficulty", difficulty);
        queryWrapper.eq(correctCnt != null && correctCnt >= 0,"correctCnt", correctCnt);
        queryWrapper.eq(writeCnt != null && writeCnt >= 0,"writeCnt", writeCnt);
        queryWrapper.eq(status != null && status >= 0,"status", status);
        queryWrapper.orderBy(SqlUtils.validSortField(sortField), sortOrder.equals(CommonConstant.SORT_ORDER_ASC),
                sortField);

        return queryWrapper;
    }

    @Override
    public long questionAdd(String content, String selection, String answer, Integer score, String analysis, String remark, String questionType, String type, Integer difficulty, Integer status) {
        if(StringUtils.isAnyBlank(content, selection, answer, questionType)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }

        if (score == null || difficulty == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"参数为空");
        }

        if (score == 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"题目分值不能为0");
        }

        synchronized (questionType){
            Question question = new Question();
            question.setContent(content);
            question.setSelection(selection);
            question.setAnswer(answer);
            question.setAnalysis(analysis);
            question.setRemark(remark);
            question.setScore(score);
            question.setQuestionType(questionType);
            question.setType(type);
            question.setDifficulty(difficulty);
            question.setStatus(status);
//            question.setCreateTime();
//            question.setUpdateTime();
            boolean save = this.save(question);
            if(!save){
                throw new BusinessException(ErrorCode.SYSTEM_ERROR,"添加失败，数据库错误");
            }
            return question.getId();
        }
    }
}




